import { ref } from "vue";
import { createBaseTool } from "../core/BaseTool";
import { EaserTool } from "../tools/EaserTool";
import { PenTool } from "../tools/PenTool";
import { ShapeTool } from "../tools/ShapeTool";
import { TextTool } from "../tools/TextTool";
import { App } from "leafer-ui";
import { IToolConfig } from "../types";

export const useToolManager = (app:App,config:IToolConfig) =>{
  const activeTool = ref("");
  // 工具Map
  let toolMap: Record<string, any> = {
    pen: PenTool,
    text: TextTool,
    shape: ShapeTool,
    easer: EaserTool,
  };
  let toolManager: any;
  // 切换工具
  const setActiveTool = (toolName: string) => {
    if (activeTool.value === toolName) return;
    toolManager?.unregisterEvents?.();
    activeTool.value = toolName;
    const tool = toolMap[toolName];
    if (!tool) return;
    // toolManager = createBaseTool(app, tool({app, config,historyUtil}) );
    // if(isFunction(tool)){
    //   toolManager = createBaseTool(app, toolMap[toolName]({app, config,historyUtil}) );
    //   toolMap[toolName] = toolManager;
    // }else {
    //   toolManager = tool;
    // }
    toolManager.registerEvents();
  };
  return { activeTool, setActiveTool };
}